package com.black.web.controller.exercise;

import com.black.app.domain.req.exercise.add.AppExerciseSeriesAddReq;
import com.black.app.domain.req.exercise.edit.AppExerciseSeriesEditReq;
import com.black.app.domain.req.exercise.query.AppExerciseActionQueryReq;
import com.black.app.domain.req.exercise.query.AppExerciseSeriesQueryReq;
import com.black.app.domain.resp.exercise.AdminExerciseActionResp;
import com.black.app.domain.resp.exercise.AdminExerciseSeriesResp;
import com.black.app.domain.resp.exercise.AppTypeTreeResp;
import com.black.app.service.exercise.AppExerciseActionService;
import com.black.app.service.exercise.AppExerciseSeriesService;
import com.black.app.service.exercise.AppExerciseTypeService;
import com.black.common.annotation.Log;
import com.black.common.core.controller.BaseController;
import com.black.common.core.domain.R;
import com.black.common.enums.BusinessType;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@Api(tags = "后台-锻炼相关Api")
@RestController
@RequestMapping("/admin/exercise/series")
public class AdminExerciseSeriesController extends BaseController {

    @Autowired
    private AppExerciseTypeService appExerciseTypeService;

    @Autowired
    private AppExerciseSeriesService appExerciseSeriesService;

    @PreAuthorize("@ss.hasPermi('exercise:type:list')")
    @ApiOperation(value = "获取锻炼类型树")
    @GetMapping(value = "/typeTree")
    public R<List<AppTypeTreeResp>> typeTree() {
        List<AppTypeTreeResp> treeResp = this.appExerciseTypeService.allTypeTree();
        return R.ok(treeResp);
    }

    @PreAuthorize("@ss.hasPermi('exercise:series:list')")
    @ApiOperation(value = "分页查询锻炼系列")
    @GetMapping("/list")
    public R<PageInfo<AdminExerciseSeriesResp>> list(AppExerciseSeriesQueryReq req) {
        PageInfo<AdminExerciseSeriesResp> page = this.appExerciseSeriesService.paginationQuery(req);
        return R.ok(page);
    }

    @PreAuthorize("@ss.hasPermi('exercise:series:query')")
    @ApiOperation(value = "根据id查询系列")
    @GetMapping("/{seriesId}")
    public R<AdminExerciseSeriesResp> findById(@PathVariable(value = "seriesId") Long seriesId) {
        AdminExerciseSeriesResp appExerciseSeries = this.appExerciseSeriesService.findById(seriesId);
        return R.ok(appExerciseSeries);
    }

    @Log(title = "删除系列", businessType = BusinessType.DELETE)
    @PreAuthorize("@ss.hasPermi('exercise:series:remove')")
    @ApiOperation(value = "删除系列")
    @DeleteMapping(value = "/{ids}")
    public R delete(@PathVariable(value = "ids") Long[] ids) {
        boolean res = this.appExerciseSeriesService.deleteById(ids);
        return super.isSuccess(res);
    }

    @Log(title = "添加系列", businessType = BusinessType.INSERT)
    @PreAuthorize("@ss.hasPermi('exercise:series:add')")
    @ApiOperation(value = "添加系列")
    @PostMapping
    public R add(@RequestBody @Valid AppExerciseSeriesAddReq req) {
        boolean res = this.appExerciseSeriesService.add(req);
        return super.isSuccess(res);
    }

    @Log(title = "修改系列", businessType = BusinessType.UPDATE)
    @PreAuthorize("@ss.hasPermi('exercise:series:edit')")
    @ApiOperation(value = "修改系列")
    @PutMapping
    public R edit(@RequestBody @Valid AppExerciseSeriesEditReq req) {
        boolean res = this.appExerciseSeriesService.edit(req);
        return super.isSuccess(res);
    }
}
